/*
 * @Author: Vorga
 * @LastEditors: Vorga
 */
#ifndef NETWORK_H
#define NETWORK_H

#include <sys/socket.h>
#include <arpa/inet.h>

typedef struct sockaddr* SAP;

typedef enum NetType
{
    TCP = 1,
    UDP,
    NetTypeNum
}NetType;

typedef enum NetRole
{
    Server,
    Client,
    NetRoleNum,
}NetRole;

typedef struct NetWork
{
    int sockfd;
    int type;
    NetRole role;
    struct sockaddr_in addr;
    socklen_t addrlen;
}NetWork;

/**
 * @description:
 * @param {NetType} type
 * @param {uint16_t} port
 * @param {char*} ip
 * @param {NetRole} role
 * @return {*}
 */
NetWork* open_nw(NetType type, uint16_t port, const char* ip, NetRole role);

/**
 * @description:
 * @param {NetWork*} nw
 * @return {*}
 */
NetWork* accept_nw(NetWork* nw);

/**
 * @description:
 * @param {NetWork*} nw
 * @param {void*} buf
 * @param {size_t} len
 * @return {*}
 */
ssize_t send_nw(NetWork* nw, void* buf, size_t len);

/**
 * @description:
 * @param {NetWork*} nw
 * @param {void*} buf
 * @param {size_t} len
 * @return {*}
 */
ssize_t recv_nw(NetWork* nw, void* buf, size_t len);

/**
 * @description:
 * @param {NetWork*} nw
 * @return {*}
 */
int close_nw(NetWork* nw);

char* getip_local(void);

const char* getip_nw(NetWork* nw);

#endif //NETWORK_H